package vn.edu.saigontech.ncms.web.offering;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.OfferingMember;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.studying.AbsenceRecord;
import vn.edu.saigontech.ncms.service.AbsenceRecordService;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.ReceiptService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class AbsenceAction extends BaseAction {
	private Long id;
	private Long offeringId;
	private Long memberId;
	private int numOfAbsentDays;
	private String contactingDate;
	private String reason;
	
	public String showAdd() {
		REQUEST().setAttribute("members", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		return SUCCESS;
	}
	
	public String doAdd() {
		AbsenceRecord absenceRecord = new AbsenceRecord();
		OfferingMember member = OfferingMemberService.INSTANCE.findById(this.memberId);
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		
		absenceRecord.setOfferingMember(member);
		absenceRecord.setNumOfAbsentDays(numOfAbsentDays);
		absenceRecord.setContactingDate(DateUtil.fromString(this.contactingDate, this.i18n("format.date")));
		absenceRecord.setReason(reason);
		absenceRecord.setOffering(offering);
		absenceRecord = AbsenceRecordService.INSTANCE.persist(absenceRecord);
		
		String studentName = member.getReceipt().getStudent().getFullName();
		this.addLoggingDetail(BaseAction.ADD, "logging.absence.add", studentName, offering.getName());
		return SUCCESS;
	}
	
	public String showEdit() {
		AbsenceRecord absenceRecord = AbsenceRecordService.INSTANCE.findById(this.id);
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("members", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("model", absenceRecord);
		return SUCCESS;
	}
	
	public String doEdit() {
		AbsenceRecord absenceRecord = AbsenceRecordService.INSTANCE.findById(this.id);
		String oldStudentName = absenceRecord.getOfferingMember().getReceipt().getStudent().getFullName(); 
		OfferingMember member = OfferingMemberService.INSTANCE.findById(this.memberId);
		absenceRecord.setOfferingMember(member);
		absenceRecord.setNumOfAbsentDays(numOfAbsentDays);
		absenceRecord.setContactingDate(DateUtil.fromString(this.contactingDate, this.i18n("format.date")));
		absenceRecord.setReason(reason);
		
		absenceRecord= AbsenceRecordService.INSTANCE.persist(absenceRecord);
		if(Checker.isNotNull(absenceRecord)){
			String studentName = member.getReceipt().getStudent().getFullName();
			Offering offering = absenceRecord.getOffering();
			this.addLoggingDetail(BaseAction.EDIT, "logging.absence.edit", oldStudentName , studentName, offering.getName());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		AbsenceRecord absenceRecord = AbsenceRecordService.INSTANCE.findById(this.id);
		Offering offering = absenceRecord.getOffering();
		boolean ret = AbsenceRecordService.INSTANCE.deleteById(this.id);
		if(ret) {
			Student student = ReceiptService.INSTANCE.findStudentByOfferingMemberId(absenceRecord.getOfferingMember().getId());
			this.addLoggingDetail(BaseAction.DELETE, "logging.absence.delete", student.getFullName(), offering.getName());
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String list() {
		return SUCCESS;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getNumOfAbsentDays() {
		return numOfAbsentDays;
	}

	public void setNumOfAbsentDays(int numOfAbsentDays) {
		this.numOfAbsentDays = numOfAbsentDays;
	}
	
	public String getContactingDate() {
		return contactingDate;
	}

	public void setContactingDate(String contactingDate) {
		this.contactingDate = contactingDate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}
}
